import QtQuick 1.0
import com.nokia.symbian 1.1
import "../delegates"
import "../js/core.js" as Core

Page {
    id: root

    property string quizTopicName
    property int quizTopicID

    property int count

    TitleHeader {
        id: header;
        text: quizTopicName + " - Edit Questions"
    }

    ListModel {
        id: questionModel
    }

    ListView {
        id:  questionView
        anchors {
            left: root.left; right: root.right;
            top: header.bottom; bottom: parent.bottom;
        }
        model: questionModel
        delegate: QuizItemDelegate {
            id: itemDelegate
            questionText: model.question
            answerText: model.answer
            onClicked: {
                quizTopicEditDialog.open();
                quizTopicEditDialog.questionInput = questionText;
                quizTopicEditDialog.answerInput  = answerText;
                quizTopicEditDialog.questionID = model.questionID;
            }
            onCheckBoxClicked: {
                if(checked != model.checked) {
                    questionModel.setProperty(index, 'checked', checked);
                    Core.updateQuestionChecked(model);
                }
            }
        }
        clip: true
    }

    QuestionInputDialog{
        id: quizTopicCreateDialog
        dialogTitleText: "Add Question"
        questionInput: "Enter Question"
        answerInput: "Enter Answer"

        onAccepted: {
            if(questionInput !== "" && answerInput != "") {
                Core.addQuestion(questionInput, answerInput, quizTopicID);
                updateUi();
            }
        }
    }

    QuestionInputDialog{
        id: quizTopicEditDialog
        dialogTitleText: "Edit Question"
        questionInput: "Enter Question"
        answerInput: "Enter Answer"
        property int questionID


        onAccepted: {
            if(questionInput !== "" && answerInput != "") {
                Core.updateQuestion(questionInput, answerInput, questionID);
                updateUi();
            }
        }
    }

    MessageDialog{
        id: msgDialog

        dialogTitleText: "Error"
        messageText:"Must have at least\n   5 questions to\n        proceed"

        onAccepted: {
                updateUi();
        }
    }

    ConfirmDialog{
        id:deleteDialog
        dialogTitleText: "Delete Confirmation"
        messageText: "          Are you sure you want\n to delete the selected questions?"

        onAccepted: {
            Core.deleteQuestions();
            updateUi();
        }
    }

    tools: ToolBarLayout {
        ToolButton{
            iconSource: "toolbar-back"
            onClicked:
                root.pageStack.pop();
        }
        ToolButton{
            iconSource: "toolbar-add"
            onClicked:
                quizTopicCreateDialog.open();
        }
        ToolButton{
            iconSource: "toolbar-delete"
            onClicked:{
                deleteDialog.open();
            }
        }
        ToolButton{
            text:"Take Quiz"
            onClicked:{
                count = Core.countQuestions(quizTopicID);
                if(count >=5){
                    root.pageStack.push(window.questionPage, {quizTopicID: quizTopicID, quizTopicName: quizTopicName});
                }
                else{
                    msgDialog.open();
                }
            }
        }
    }

    onStatusChanged: {
        if(status == PageStatus.Activating) {
            updateUi();
        }
    }

    function updateUi()
    {
        questionView.model = 0;
        Core.readQuestion(questionModel, quizTopicID);
        questionView.model = questionModel;
    }

    Component.onCompleted: {
        Core.openDB();
    }

}
